package project.ccso.top.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.codehaus.jackson.map.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.sun.corba.se.spi.orbutil.fsm.Guard.Result;

import project.ccso.top.bean.GridPageBean;
import project.ccso.top.bean.ReturnResult;
import project.ccso.top.entity.City;
import project.ccso.top.service.CityService;
import project.ccso.top.service.impl.CityServiceImpl;

/**
 * CityServlet
 * 处理与cCity相关的请求
 * @author aibinxiao
 * @date 2018年8月23日 上午10:47:25
 */
@WebServlet("/CityServlet")
public class CityServlet extends BaseServlet {
	private static final long serialVersionUID = 1L;
	private static Logger logger = LoggerFactory.getLogger(CityServlet.class);
	// 数据json格式转换
	private static ObjectMapper mapper = new ObjectMapper();
	private CityService ciytService = new CityServiceImpl();
	
	/**
	 * 查询所有的城市*
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	public void findCityByPage(HttpServletRequest request, HttpServletResponse response) throws IOException {
		PrintWriter writer = response.getWriter();
		ReturnResult result = new ReturnResult();
		String pageNo = request.getParameter("page");
		String pageSize = request.getParameter("rows");
		String cityId = request.getParameter("id");
		String cityName = request.getParameter("name");
		String provinceId = request.getParameter("provinceId");
		String startDate = request.getParameter("startDate");
		String endDate = request.getParameter("endDate");
		GridPageBean<City> pageBean = null;
		try {
			pageBean = ciytService.findCityByPage(pageNo, pageSize, cityId,cityName,provinceId,startDate,endDate);
			result.setResult(project.ccso.top.enumerate.Result.SUCCESS.name());
			result.setMessage("查询第" + pageNo + "页的城市信息成功！");
		} catch (Exception e) {
			result.setResult(project.ccso.top.enumerate.Result.FAILED.name());
			result.setMessage(e.getMessage());
			e.printStackTrace();
		}
		result.setData(pageBean);
		writer.print(mapper.writeValueAsString(result));
	}
	
	/**
	 * 根据provinceId查询所有城市的信息*
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	public void selectByProvinceId(HttpServletRequest request, HttpServletResponse response) throws IOException {
		PrintWriter writer = response.getWriter();
		ReturnResult result = new ReturnResult();
		String provinceId = request.getParameter("provinceId");
		List<City> list = null;
		try {
			list = ciytService.selectByProvinceId(provinceId);
			result.setResult(project.ccso.top.enumerate.Result.SUCCESS.name());
			result.setMessage("根据provicneId查询城市信息成功！");
		} catch (Exception e) {
			result.setResult(project.ccso.top.enumerate.Result.FAILED.name());
			result.setMessage(e.getMessage());
			e.printStackTrace();
		}
		result.setData(list);
		writer.print(mapper.writeValueAsString(result));
	}
	
}
